home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 52811 / 52811.xpi / chrome / content / AES / utf-8.js < prev   
Encoding:
JavaScript  |  2009-07-03  |  3.4 KB  |  101 lines

  1. /*    Encoding and decoding of Unicode character strings as    UTF-8 byte streams.*/
  2.  
  3. //    UNICODE_TO_UTF8  --  Encode Unicode argument string as UTF-8 return value
  4. function unicode_to_utf8(s) {
  5.     var utf8 = "";
  6.     
  7.     for (var n = 0; n < s.length; n++) {
  8.     var c = s.charCodeAt(n);//presa la posizione di un carattere ritorna il numero associato
  9.     
  10.     if (c <= 0x7F) { //fine Regular ASCII chart
  11.       //  0x00 - 0x7F:  Emit as single byte, unchanged
  12.       utf8 += String.fromCharCode(c);
  13.     } 
  14.     else if ((c >= 0x80) && (c <= 0x7FF)) { //dalla Extended ASCII chart in poi
  15.       //  0x80 - 0x7FF:  Output as two byte code, 0xC0 in first byte
  16.       //                                               0x80 in second byte
  17.       utf8 += String.fromCharCode((c >> 6) | 0xC0); //il carattere lo scmpongo in 2 parti
  18.       utf8 += String.fromCharCode((c & 0x3F) | 0x80);
  19.     } 
  20.     else {
  21.       // 0x800 - 0xFFFF:  Output as three bytes, 0xE0 in first byte
  22.       //                                                   0x80 in second byte
  23.       //                                                   0x80 in third byte
  24.       utf8 += String.fromCharCode((c >> 12) | 0xE0);
  25.       utf8 += String.fromCharCode(((c >> 6) & 0x3F) | 0x80);
  26.       utf8 += String.fromCharCode((c & 0x3F) | 0x80);
  27.     }
  28.     }
  29.     return utf8;
  30. }
  31.  
  32. //    UTF8_TO_UNICODE  --  Decode UTF-8 argument into Unicode string return value
  33. function utf8_to_unicode(utf8) {
  34.     var s = "", i = 0, b1, b2, b2;
  35.  
  36.     while (i < utf8.length) {
  37.     b1 = utf8.charCodeAt(i);
  38.     if (b1 < 0x80) {        // One byte code: 0x00 0x7F
  39.         s += String.fromCharCode(b1);
  40.         i++;
  41.     } 
  42.     else if((b1 >= 0xC0) && (b1 < 0xE0)) {    // Two byte code: 0x80 - 0x7FF
  43.         b2 = utf8.charCodeAt(i + 1);
  44.         s += String.fromCharCode(((b1 & 0x1F) << 6) | (b2 & 0x3F));
  45.         i += 2;
  46.     } 
  47.     else {                // Three byte code: 0x800 - 0xFFFF
  48.         b2 = utf8.charCodeAt(i + 1);
  49.       b3 = utf8.charCodeAt(i + 2);
  50.         s += String.fromCharCode(((b1 & 0xF) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F));
  51.         i += 3;
  52.     }
  53.     }
  54.     return s;
  55. }
  56.  
  57. /*    ENCODE_UTF8  --  Encode string as UTF8 only if it contains
  58. a character of 0x9D (Unicode OPERATING
  59. SYSTEM COMMAND) or a character greater
  60. than 0xFF.  This permits all strings
  61. consisting exclusively of 8 bit
  62. graphic characters to be encoded as
  63. themselves.  We choose 0x9D as the sentinel
  64. character as opposed to one of the more
  65. logical PRIVATE USE characters because 0x9D
  66. is not overloaded by the regrettable
  67. "Windows-1252" character set.  Now such characters
  68. don't belong in JavaScript strings, but you never
  69. know what somebody is going to paste into a
  70. text box, so this choice keeps Windows-encoded
  71. strings from bloating to UTF-8 encoding.  */     
  72. function encode_utf8(s) {
  73.   var i, necessary = false;
  74.     
  75.     for (i = 0; i < s.length; i++) {
  76.         if ((s.charCodeAt(i) == 0x9D) || (s.charCodeAt(i) > 0xFF)) {
  77.             necessary = true; //la codifica Φ necessaria
  78.             break;
  79.         }
  80.     }
  81.     if (!necessary) {
  82.         return s;
  83.     }
  84.     return String.fromCharCode(0x9D) + unicode_to_utf8(s);
  85. } //              ^^-preso un numero ritorna il carattere associato (pivot)
  86.     
  87. /*  DECODE_UTF8  --  Decode a string encoded with encode_utf8
  88. above.  If the string begins with the
  89. sentinel character 0x9D (OPERATING
  90. SYSTEM COMMAND), then we decode the
  91. balance as a UTF-8 stream.  Otherwise,
  92. the string is output unchanged, as
  93. it's guaranteed to contain only 8 bit
  94. characters excluding 0x9D.  */         
  95. function decode_utf8(s) {
  96.   if ((s.length > 0) && (s.charCodeAt(0) == 0x9D)) {
  97.         return utf8_to_unicode(s.substring(1));
  98.     }
  99.     return s;
  100. }
  101.